package com.lleice.common.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.lleice.common.annotation.Permission;
import com.lleice.common.annotation.RequestLog;
import com.lleice.common.entity.base.CommonEntity;
import com.lleice.common.entity.page.PageReq;

import io.swagger.annotations.ApiOperation;

/**
 * 父类controller
 * @author lizihao
 *
 * @param <Service> 生成的service
 * @param <Entity> 生成的实体类
 * @param <PageEntity> 分页参数实体
 */
public abstract class BaseController<Service extends IService<Entity>, Entity extends CommonEntity,PageEntity extends PageReq<Entity>> {

	@Autowired
	protected Service service;
	
	@ApiOperation(value = "新增或修改")
	@Permission
	@RequestLog
    @RequestMapping(value = "/addOrUpdate", method = RequestMethod.POST)
	public Boolean add(@Validated @RequestBody Entity entity) throws Exception{
		service.saveOrUpdate(entity);
	 return true;
 }
	
	@ApiOperation(value = "删除")
	@Permission
	@RequestLog
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
	public Boolean delete(@RequestParam("id") Long id) throws Exception{
		QueryWrapper<Entity> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("id", id);
		queryWrapper.eq("is_delete", 1);
		service.update(queryWrapper);
	 return true;
 }
	
	@ApiOperation(value = "分页")
	@Permission
	@RequestLog
    @RequestMapping(value = "/page", method = RequestMethod.POST)
	public IPage<Entity> page(@RequestBody PageEntity pageReq) throws Exception{
		QueryWrapper<Entity> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("is_delete", 0);
	 return service.page(pageReq.getPage(), queryWrapper);
 }
	
	@ApiOperation(value = "所有列表")
	@Permission
	@RequestLog
    @RequestMapping(value = "/all", method = RequestMethod.POST)
	public List<Entity> pageAll() throws Exception{
	 return service.list();
 }
}
